package se.study.algorithm;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {
	private static int length=128;
	/**
	 * 加密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws UnsupportedEncodingException
	 * @throws InvalidKeyException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 */
	private static byte[] encrypt(String content, String password)
			throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" ); 
                secureRandom.setSeed(password.getBytes()); 
		kgen.init(length, secureRandom);
		SecretKey secretKey = kgen.generateKey();
		byte[] enCodeFormat = secretKey.getEncoded();
		SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
		Cipher cipher = Cipher.getInstance("AES");// 创建密码器
		byte[] byteContent = content.getBytes("utf-8");
		cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
		byte[] result = cipher.doFinal(byteContent);
		return result; // 加密
	}

	/**
	 * 解密
	 * 
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	private static byte[] decrypt(byte[] content, String password)
			throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
                 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" ); 
                  secureRandom.setSeed(password.getBytes()); 
		kgen.init(length, secureRandom);
		SecretKey secretKey = kgen.generateKey();
		byte[] enCodeFormat = secretKey.getEncoded();
		SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
		Cipher cipher = Cipher.getInstance("AES");// 创建密码器
		cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
		byte[] result = cipher.doFinal(content);
		return result; // 加密
	}

	public static String aesEncrypt(String content, String password){
		try {
			byte[] encryptResult = encrypt(content, password);
			return new BASE64Encoder().encode(encryptResult);
		} catch (Exception e) {}
		return null;
	}

	public static String aesDecrypt(String content, String password){
		try {
			byte[] decryptResult = decrypt(new BASE64Decoder().decodeBuffer(content), password);
			return new String(decryptResult,"UTF-8");
		} catch (Exception e) {}
		return null;
	}

	public static void main(String[] args) throws Exception {
		String content = "测试数据";
		String password = Constants.AES_KEY;
		// 加密
		System.out.println("加密前：" + content);
		String tt4 = aesEncrypt(content, password);
		System.out.println(new String(tt4));
		// 解密
		String d = aesDecrypt(tt4, password);
		System.out.println("解密后：" + d);
	}
}
